import { request } from "@umijs/max";
import { saveAs } from "file-saver";
import { blobValidate } from ".";
import { message } from "antd";
// 通用下载方法
export async function download(
  url: any,
  params: any,
  filename: any,
  config?: any
) {
  const res = await request(url, {
    method: "POST",
    data: params,
    params,
    responseType: "blob",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    ...(config || {}),
  });

  // 判断是否是blob格式
  const isBlob = blobValidate(res);
  console.log(isBlob);
  if (isBlob) {
    const blob = new Blob([res as any]);
    saveAs(blob, filename);
  } else {
    const resText = await res?.data?.text();
    const rspObj = JSON.parse(resText);
    // const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
    message.error(resText);
  }
}
